package main

import (
	"errors"
	"executeserver/src/model"
	"executeserver/src/worker"
	"log"
	"reflect"
	"time"

	"github.com/go-redis/redis"
)

var i = 0

func main() {
	log.Println("start")
	tick := time.NewTicker(1 * time.Second)
	// 启动协程 处理任务
	go func() {
		for {
			<-tick.C
			log.Println("1s打印一次消息")

		}
	}()
	i := 1
	for {
		i++
		if i == 5 {
			log.Println("cleardata...")
			worker.ClearData(1)
		}
		if i == 100 {
			i = 5
		}
	}
}
func test1() {
	log.Println("test")
}
func retry(v interface{}) interface{} {
	// log.Println(reflect.TypeOf(v), strings.HasPrefix(reflect.TypeOf(v).String(), "func"))
	return func(params ...interface{}) (result []reflect.Value) {
		f := reflect.ValueOf(v)
		in := make([]reflect.Value, len(params))
		for k, param := range params {
			in[k] = reflect.ValueOf(param)
		}
		for i := 0; i < 3; i++ {
			result = f.Call(in)
			log.Println(result)
		}
		return
	}

}

func test(i int) error {
	log.Println("test")
	if i == 3 {
		return errors.New("runtime error")
	}
	return nil
}

func testRedisWatch() {
	// redisStr := `{"host":"127.0.0.1","port":6379,"password":"aa778899"}` // `{"host":"127.0.0.1,"port":6379,"password":"aa778899"}`
	localRedis := model.GetLocalRedis()
	clearKey := `statusInfo_1`
	fn := func(tx *redis.Tx) error {
		_, err := localRedis.HGet(clearKey, "0").Result()
		if err != nil {

			return err
		}
		time.Sleep(20 * time.Second)

		_, hsetErr := tx.Pipelined(func(pipe redis.Pipeliner) error {
			data, err := pipe.HSet(clearKey, "0", "test100").Result()
			log.Println("pipeline", data, err)

			return nil
		})
		log.Println("hseterror", hsetErr)
		return hsetErr

	}

	err := localRedis.Watch(fn, clearKey)
	// go func() {
	// 	time.Sleep(5 * time.Second)
	// 	res, resErr := localRedis.HSet(clearKey, "0", "test").Result()

	// 	log.Println("routine", res, resErr)
	// }()
	log.Println(err)
	// for {
	// }

}
